package com.sunyu.springboot.controller;


import com.power.common.model.CommonResult;
import com.power.common.util.DateTimeUtil;
import com.power.common.util.UUIDUtil;
import com.sunyu.springboot.component.RestTemplateClient;
import com.sunyu.springboot.model.User;
import com.sunyu.springboot.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.util.HashMap;

/**
 * 用户操作接口
 *
 * @author yu
 * @date 2017-07-27 16:58:53
 */
@RestController
public class UserController {
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @Resource
    private RestTemplateClient restTemplateClient;

    /**
     * 根据id用户信息
     *
     * @return
     */
    @GetMapping(value = "/query")
    public CommonResult<User> queryById() {
        User user = userService.queryById(0);
        return CommonResult.ok().setResult(user);
    }

    /**
     * 获取本机ip地址
     *
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/ip")
    public CommonResult<String> getIp() throws Exception {
        InetAddress addr = InetAddress.getLocalHost();
        String ip = addr.getHostAddress().toString();
        return CommonResult.ok().setResult(ip);
    }

    /**
     * 获取时间
     *
     * @return
     */
    @GetMapping(value = "time")
    public CommonResult<String> getTime() {
        String time = DateTimeUtil.long2Str(System.currentTimeMillis(), DateTimeUtil.DATE_FORMAT_SECOND);
        return CommonResult.ok().setResult(time);
    }

    /**
     * 输出hello world
     *
     * @param name 名称
     * @return
     */
    @GetMapping("/")
    public String index() {
        return "hello docker,from v1.4.";
    }

    /**
     * 增添日志测试
     *
     * @return
     */
    @GetMapping(value = "/log")
    public CommonResult<String> testLog() {
        logger.info("info current time: {}", System.currentTimeMillis());
        logger.debug("current uuid: {}", UUIDUtil.getUuid());
        return CommonResult.ok().setResult("print log");
    }

    /**
     * 测试k8s svc
     *
     * @param svc
     * @return
     */
    @GetMapping(value = "svc/{svc}")
    public CommonResult<String> testService(@PathVariable String svc) {
        String rest = restTemplateClient.postJson(svc, new HashMap<>());
        return CommonResult.ok().setResult(rest);
    }
}